﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bunian.Core;
using Bunian.Service;
using Bunian.Core.BusinessObjects;
using Bunian.Web.Common.Code;
using System.Web.UI.HtmlControls;

namespace Bunian.Web.Controls
{
    public partial class CIndividualProfile : System.Web.UI.UserControl
    {
        public Child CurrentChild { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            BindControls();
        }

        protected void BindControls()
        {
            ltName.Text = CurrentChild.ContactInfo.FullName;

            int age = CurrentChild.GetAgeInYears();

            ltName.Text = BeneficiaryFormattingHelper.GetChildDescription(CurrentChild); //age + " years old - " + BeneficiaryFormattingHelper.GetSex(CurrentChild);

            ltNumberOfChildren.Text = BeneficiaryFormattingHelper.GetFamilyInfo(CurrentChild.Family);
            
            //ltCoveredCount.Text = BeneficiaryFormattingHelper.GetCoveredCount(CurrentChild.Family);
            
            ltProvider.Text = BeneficiaryFormattingHelper.GetProvider(CurrentChild.Family);
            
            ltFamilyTitle.Text = CurrentChild.Family.ContactDetails.FullName;

            //ltDescription.Text = CurrentChild.Family.Description;
            
            ltAddress.Text = CurrentChild.Family.ContactDetails.GetAddressByType(AddressType.StreetAddress);

            ltDescription.Text = CurrentChild.Description;
            ltDeficiency.Text = Convert.ToInt32(Math.Floor(CurrentChild.Deficiency)).ToString();
            ltSourcesOfIncome.Text = CurrentChild.SourcesOfIncome;

            //aViewFamily.HRef = "~/pages/profile.family.aspx?fid=" + CurrentChild.Family.Id;

            rptOtherFamilyMembers.ItemDataBound += new RepeaterItemEventHandler(rptOtherFamilyMembers_ItemDataBound);

            rptOtherFamilyMembers.DataSource = CurrentChild.GetOtherFamilyMembers();
            rptOtherFamilyMembers.DataBind();
        }

        protected void rptOtherFamilyMembers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            switch (e.Item.ItemType)
            {
                case ListItemType.AlternatingItem:
                case ListItemType.Item:

                    Literal ltChildTitle = (Literal)e.Item.FindControl("ltChildTitle");

                    Child currentChild = (Child)e.Item.DataItem;

                    ltChildTitle.Text = BeneficiaryFormattingHelper.GetChildDescription(CurrentChild);

                    HtmlAnchor aSibling = (HtmlAnchor)e.Item.FindControl("aSibling");
                    aSibling.HRef = "~/pages/profile.individual.aspx?cid=" + currentChild.Id;

                    break;
            }
        }
    }
}